<!DOCTYPE html>
<html ng-app="app">

<head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title></title>
    <link rel="icon" type="image/x-icon" href="" />
    <link rel="stylesheet" type="text/css" href="bower_components/font-awesome/css/font-awesome.min.css" />
    <link rel="stylesheet" href="bower_components/materialize/dist/css/materialize.min.css" />
    <link rel="stylesheet" type="text/css" href="page.css">
    <script src="lib/angular-1.5.5/angular.js" type="text/javascript" charset="utf-8"></script>
</head>
<!--[if lte IE 8]>
<body class="lower-ie">
<img src="http://qbmf.trenddata.cn/assets/img/forie.png" alt="">
<![endif]-->
<!--[if (gt IE 8)|!(IE)]><body></body><![endif]-->
<div class="center-vertical bg-black" ng-controller="loginCtr as vm" ng-keyup="vm.Enter($event)" ng-class="{'pp':vm.user.channel==3,'qzt': vm.user.channel==4}">
    <div class="navbar-fixed">
        <nav>
            <div class="nav-wrapper">
                <a href="javascript:;" class="brand-logo left"><img ng-src="{{vm.user.brand}}" alt="logo"></a>
                <ul id="nav-mobile" class="right">
                    <li class="active"><a href="forget.html">找回密码</a></li>
                    <li><a href="reg.html">注册</a></li>
                    <li><a href="login.html">登录</a></li>
                </ul>
            </div>
        </nav>
    </div>
    <div class="center-content">
        <div class="container">
            <div class="row">
                <div class="col m8 hide-on-small-only">
                    <div class="pic">
                        <img ng-src="{{vm.user.logPic}}" class="w h" />
                    </div>
                </div>
                <form class="col s12 m4 form-box">
                    <div id="login-form" class="content-box">
                        <div class="row content-box-wrapper">
                            <h1 class="title">密码重置</h1>
                            <div class="divider mb8"></div>
                            <div class="input-field col s12">
                                <input id="username" type="text" class="validate" ng-class="{'invalid':vm.error.phone.error}" name="name" ng-model="vm.user.phone" />
                                <label for="username" class="active" data-error="{{vm.error.phone.info}}">手机号</label>
                            </div>
                            <div class="input-field col s12">
                                <input id="usercode" type="text" class="validate col s8" name="name" ng-model="vm.user.code" ng-class="{'invalid':vm.error.code.error}" />
                                <label for="usercode" class="active" data-error="{{vm.error.code.info}}">验证码</label>
                                <a class="col s4 waves-effect waves-light btn hoverable" ng-click="vm.user.getCode()" ng-show="vm.scond === 60">获取验证码</a>
                                <a class="col s4 waves-effect waves-light btn disabled" ng-show="vm.scond !== 60">{{vm.scond+'秒'}}</a>
                            </div>
                            <div class="input-field col s12">
                                <input id="password" type="password" class="validate" name="password" ng-model="vm.user.password" ng-class="{'invalid':vm.error.pw.error}" />
                                <label for="password" class="active" data-error="{{vm.error.pw.info}}">设置密码</label>
                            </div>
                            <div class="col s12">
                                <div class="waves-effect waves-light btn col s12 hoverable" ng-click="vm.user.submit()">密码重置</div>
                            </div>
                        </div>
                    </div>
            </div>
            </form>
        </div>
    </div>
</div>
<ng-atomic-notify></ng-atomic-notify>
<script src="bower_components/angular-atomic-notify/dist/angular-atomic-notify.min.js"></script>
<script type="text/javascript">
(function() {

    angular.module('app', ['atomic-notify']).controller('loginCtr', loginCtr);

    loginCtr.$inject = ['$http', '$q', '$timeout', 'atomicNotifyService'];

    function loginCtr($http, $q, $timeout, atomicNotifyService) {
        var vm = this;

        vm.error = {
            phone: {
                error: false,
                info: ''
            },
            code: {
                error: false,
                info: ''
            },
            pw: {
                error: false,
                info: ''
            }
        }

        vm.user = {
            brand: '',
            logPic: '',
            channel: 0,

            phone: '',
            password: '',
            code: '',
            getCode: _getCode,
            submit: _submit
        }


        init();



        function init() {
            if (window.location.href.indexOf('shop.630game.com') != -1) {

                document.title = "电商先生";

                document.querySelector('link[rel=icon]').href = "http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/icon.png";

                vm.user.brand = "./img/dsxs/icon.png";

                vm.user.logPic = 'http://qbmf.trenddata.cn/assets/img/text.png';

                vm.user.channel = 2;
            } else if (window.location.href.indexOf('esm.fenxiao8.com') != -1) {

                document.title = "品谱大数据";

                document.querySelector('link[rel=icon]').href = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.brand = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.channel = 3;

                vm.user.logPic = 'http://www.hzbigdate.com/assets/img/pingpu-text.jpg';

            } else if (window.location.href.indexOf('hzbigdate.com') != -1) {

                document.title = "品谱大数据";

                document.querySelector('link[rel=icon]').href = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.brand = "http://www.hzbigdate.com/assets/img/pingpu.png";

                vm.user.channel = 3;

                vm.user.logPic = 'http://www.hzbigdate.com/assets/img/pingpu-text.jpg';

            } else if (window.location.href.indexOf('qzt.sinoac.org') != -1) {

                document.title = "企智通";

                document.querySelector('link[rel=icon]').href = "http://qzt.sinoac.org/assets/img/qzt/qzt_ico.png";

                vm.user.brand = "http://qzt.sinoac.org/assets/img/qzt/qzt_brand.jpg";

                vm.user.channel = 4;

                vm.user.logPic = 'http://qzt.sinoac.org/assets/img/qzt/banner.png';

            } else {
                document.title = "情报魔方";
                document.querySelector('link[rel=icon]').href = 'http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/favicon.ico';



                vm.user.brand = "./img/logo.png";

                vm.user.logPic = 'http://trendata.oss-cn-hangzhou.aliyuncs.com/marketconditions/slogan.png';

                vm.user.channel = 0;
            }


        }


        vm.scond = 60;

        function setTime() {
            if (vm.scond !== 0) {
                vm.scond--;
                $timeout(function() {
                    setTime();
                }, 1000);
            } else {
                vm.scond = 60;
            }
        }

        function _getCode() {
            console.log(this);
            this.phone = this.phone.replace(/(^\s*)|(\s*$)/g, "");
            console.log(this.phone);
            if (this.phone.length == 0) {

                vm.error.phone.error = true;
                vm.error.phone.info = "请输入手机号";

            } else if (!(/^1[34578]\d{9}$/.test(this.phone))) {

                vm.error.phone.error = true;
                vm.error.phone.info = "手机号码格式错误";

            } else {

                vm.error.phone.error = false;
                vm.error.phone.info = "";

                //设置倒计时
                setTime();

                $http({
                    method: 'post',
                    url: '/qbmf/sendVerifySMS',
                    data: {
                        type: 1,
                        phone: vm.user.phone,
                        channel: vm.user.channel
                    },
                    headers: {
                        'Accept': 'application/json, text/javascript, */*',
                        'Content-Type': 'application/x-www-form-urlencoded'
                    },
                    transformRequest: function(obj) {
                        var str = [];
                        for (var p in obj) {
                            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                        }
                        return str.join("&");
                    }
                }).success(function(resp) {

                    if (resp.code === 0) {



                    } else {

                        vm.error.phone.error = true;
                        vm.error.phone.info = resp.msg;
                    }
                });

            }
        }

        function _submit() {



            this.phone = this.phone.replace(/(^\s*)|(\s*$)/g, "");
            console.log(this.phone);
            if (this.phone.length == 0) {

                vm.error.phone.error = true;
                vm.error.phone.info = "请输入手机号";

                return;

            }
            if (!(/^1[34578]\d{9}$/.test(this.phone))) {

                vm.error.phone.error = true;
                vm.error.phone.info = "手机号码格式错误";
                return;
            }
            vm.error.phone.error = false;

            this.code = this.code.replace(/(^\s*)|(\s*$)/g, "");
            if (this.code.length == 0) {

                vm.error.code.error = true;
                vm.error.code.info = "请输入验证码";
                return;
            }
            vm.error.code.error = false;


            this.password = this.password.replace(/(^\s*)|(\s*$)/g, "");
            if (this.password.length == 0) {

                vm.error.pw.error = true;
                vm.error.pw.info = "请输入密码";
                return;
            }
            vm.error.pw.error = false;


            if (vm.error.code.error) {
                return;
            }

            _login(vm.user).then(function(resp) {

                vm.error.error = false;

                atomicNotifyService.info('密码重置成功', 1000);

                $timeout(function() {

                    window.location.href = 'login.html';

                }, 900);

            }, function(err) {

                vm.error.code.error = true;
                vm.error.code.info = err.msg;
            });

        }

        function _login(user) {

            var defer = $q.defer();

            $http({
                method: 'post',
                url: '/qbmf/updatePassword',
                data: {
                    nick: user.phone,
                    code: user.code,
                    password: user.password,
                    channel: vm.user.channel
                },
                headers: {
                    'Accept': 'application/json, text/javascript, */*',
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                transformRequest: function(obj) {
                    var str = [];
                    for (var p in obj) {
                        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                    }
                    return str.join("&");
                }
            }).success(function(resp) {

                if (resp.code === 0) {

                    defer.resolve(resp);

                } else {

                    defer.reject(resp)
                }
            });

            return defer.promise;
        }


    }


})();
</script>
</body>

</html>
